package com.bytz.modules.cms.productLine.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bytz.common.constant.CommonConstant;
import com.bytz.common.exception.BytzBootException;
import com.bytz.common.system.query.MPJLambdaWrapperEx;
import com.bytz.common.system.query.MPJQueryGenerator;
import com.bytz.modules.cms.product.entity.Product;
import com.bytz.modules.cms.product.service.IProductService;
import com.bytz.modules.cms.productLine.entity.ProductLineLevel;
import com.bytz.modules.cms.productLine.entity.ProductLineLevelLimit;
import com.bytz.modules.cms.productLine.mapper.ProductLineLevelLimitMapper;
import com.bytz.modules.cms.productLine.model.LevelLimitImportRequest;
import com.bytz.modules.cms.productLine.model.ProductLineLevelLimitModel;
import com.bytz.modules.cms.productLine.service.IAdminProductLineLevelLimitService;
import com.bytz.modules.cms.productLine.service.IProductLineLevelService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class AdminProductLineLevelLimitServiceImpl extends ServiceImpl<ProductLineLevelLimitMapper, ProductLineLevelLimit> implements IAdminProductLineLevelLimitService {

    @Autowired
    private IProductLineLevelService productLineLevelService;

    @Autowired
    private IProductService productService;

    @Override

    public IPage<ProductLineLevelLimitModel> page(Page<ProductLineLevelLimit> requestPage, ProductLineLevelLimitModel productLineLevelLimit, Map<String, String[]> parameterMap) throws Exception {

        MPJLambdaWrapperEx<ProductLineLevelLimit> wrapper = MPJQueryGenerator.initQueryWrapper(productLineLevelLimit, parameterMap, ProductLineLevelLimit.class);

        return baseMapper.selectJoinPage(requestPage, ProductLineLevelLimitModel.class, wrapper
                .selectAll(ProductLineLevelLimit.class)
                .select(Product::getMaterialDesc,
                        Product::getProductLineName,
                        Product::getPlant,
                        Product::getBasePrice,
                        Product::getStatus)
                .innerJoin(Product.class, Product::getId, ProductLineLevelLimit::getProductId)
                .eq(Product::getDelFlag, CommonConstant.DEL_FLAG_0)
        );
    }

    @Override
    public Boolean importByNumbers(LevelLimitImportRequest request) {

        List<String> numbers = StrUtil.split(request.getProductNumbers(), "\n").stream()
                .filter(StringUtils::isNotBlank)
                .distinct()
                .collect(Collectors.toList());

        if (CollectionUtil.isEmpty(numbers)) {
            return true;
        }

        Set<String> repeatingNumbers = lambdaQuery()
                .eq(ProductLineLevelLimit::getProductLineLevelId, request.getLevelId())
                .in(ProductLineLevelLimit::getProductNumber, numbers)
                .list()
                .stream()
                .map(ProductLineLevelLimit::getProductNumber)
                .collect(Collectors.toSet());

        numbers.removeAll(repeatingNumbers);

        Map<String, Product> productMap = productService.getByProductNumbers(numbers)
                .stream().collect(Collectors.toMap(Product::getProductNumber, t -> t));
        ProductLineLevel productLineLevel = productLineLevelService.getById(request.getLevelId());

        List<ProductLineLevelLimit> insertList = numbers.stream().distinct().map(productNumber -> {

            ProductLineLevelLimit levelLimit = new ProductLineLevelLimit();

            levelLimit.setProductLineLevelId(request.getLevelId());
            levelLimit.setLevelName(productLineLevel.getLevelName());

            Product product = productMap.get(productNumber);
            if (product == null) {
                throw new BytzBootException(String.format("找不到产品【%s】", productNumber));
            }

            levelLimit.setProductId(product.getId());
            levelLimit.setProductNumber(productNumber);
            levelLimit.setProductDesc(product.getMaterialDesc());
            levelLimit.setRemark(product.getRemark());

            return levelLimit;
        }).collect(Collectors.toList());

        return saveBatch(insertList);
    }
}
